package com.gm.commonlib.module.image.takeimage;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;


import com.github.chrisbanes.photoview.PhotoView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;
import com.gm.commonlib.R;
import com.gm.commonlib.module.image.ImageUtil;


public class ShowBigImagePopWindow {
    PopupWindow mPopupWindow;
    public PhotoView mPopupWinImage;
    public Button showBigImage;
    public ShowBigImagePopWindow(Context context) {
        init(context);
    }


    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.show_big_image_view, null, false);
        showBigImage=view.findViewById(R.id.showBigImage);
        mPopupWindow = new PopupWindow(context);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setContentView(view);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
            mPopupWindow.setOutsideTouchable(true);
        }
        mPopupWinImage = view.findViewById(R.id.imageView);
        /**
         *  this.mMinScale = 1.0F;
         *         this.mMidScale = 1.75F;
         *         this.mMaxScale = 3.0F;
         */
        mPopupWinImage.setScaleLevels(0.5f, 1.5f, 5f);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
            mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        }
        ColorDrawable dw = new ColorDrawable(0x00000000);
        mPopupWindow.setBackgroundDrawable(dw);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow != null) {
                    mPopupWindow.dismiss();
                }
            }
        });
        PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(mPopupWinImage);
        photoViewAttacher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow != null) {
                    mPopupWindow.dismiss();
                }
            }
        });

    }
    public void showBtn(boolean isShow){
        showBigImage.setVisibility(isShow?View.VISIBLE:View.GONE);
    }

    public void showImage(String url, View v) {
        mPopupWinImage.setImageResource(0);
        if (!TextUtils.isEmpty(url)) {
            ImageUtil.showImage(mPopupWinImage, url);
            mPopupWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        }

    }



}
